<?php


/** 
 *      
 * 将数组转换为JSON字符串（兼容中文）      
 * @param  array   $array      要转换的数组
 * @param  string   $func      要执行的函数  
 * @return string      转换得到的json字符串      
 * @access public      
 */     
function JSON($array,$func = 'urlencode') {         
	arrayRecursive($array, $func, true);         
	$json = json_encode($array);         
	return urldecode($json);     
}

/**        
 *        
 * 使用特定function对数组中所有元素做处理       
 * @param  string  &$array     要处理的字符串        
 * @param  string  $function   要执行的函数       
 * @return boolean $apply_to_keys_also     是否也应用到key上        
 * @access public        
 */     
function arrayRecursive(&$array, $function, $apply_to_keys_also = false){   
	static $recursive_counter = 0; 
	if (++$recursive_counter > 1000) {  
		die('possible deep recursion attack');        
	}         
	foreach ($array as $key => $value) {
		if (is_array($value)) {
			arrayRecursive($array[$key], $function, $apply_to_keys_also);
		} else {
			$value = htmlspecialchars_decode($value);
			$value = addslashes($value);
			$value =  str_replace ( "\'", "'", $value) ;
			$array[$key] = $function($value);             
		}                   
		if ($apply_to_keys_also && is_string($key)) { 
			$new_key = $function($key);            
			if ($new_key != $key) {
				$array[$new_key] = $array[$key];     
				unset($array[$key]);                
			}             
		}         
	}        
	$recursive_counter--;     
} 

/**
 * 接口统一输出
 * @param  string  $info     要输出的提示信息        
 * @param  mixed[]  $data   要输出的数据     
 * @param  string  $str     要输出的api提示信息  
 * @param  string  $sprintf  替换提示信息  
 * @return array()      json数据      
 */
function rst($info,$data = array(),$str = '',$sprintf=''){
	$errorInfo = C('API_MSG');
	if(isset($errorInfo[$info])){
		$data['erron'] = $errorInfo[$info][0];
		$data['msg'] = $str ? $str : $errorInfo[$info][1];
	}else{
		$data['erron'] = is_numeric($info) ? $info : $errorInfo['undefined_error'][0];
		$data['msg'] = $str ? $str : $errorInfo['undefined_error'][1];
	}
	
	if($sprintf){
		$data['msg'] = sprintf($data['msg'],$sprintf);
	}
    header('Content-Type:text/json; charset=UTF-8');
    exit(JSON($data));
}


/**
 * 产生一个指定长度的随机字符串,并返回给用户 
 * @param type $len 产生字符串的长度
 * @return string 随机字符串
 */
function genRandomString($len = 6,$type = 'all') {
    $letter = array(
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z"
    );
    
    $num = array("0", "1", "2","3", "4", "5", "6", "7", "8", "9");
    
    $chars = array();
    
    if($type == 'num'){
        $chars = $num ;
    }elseif($type == 'let'){
        $chars = $letter ;
    }else{
        $chars = array_merge($letter,$num);
    }
    
    $charsLen = count($chars) - 1;
    // 将数组打乱 
    shuffle($chars);
    $output = "";
    for ($i = 0; $i < $len; $i++) {
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
}

/**
 * extend静态页面加载.
 * @param   type    $tplName    模板名称
 * @param   type    $suff    模板后缀
 * @return  type    description
 * @access  public or private
 * @static  makes the class property accessible without needing an instantiation of the class
 */
function loadExtend($tplName,$suff = 'html')
{
	$tpl = APP_PATH.'/extend/order/'.$tplName.'.'.$suff;
	if(file_exists($tpl)){
		return $tpl;
	}
	return false;
} 
// end func


/**
 * xml转换为数组
 * @param string xml字符串
 * @return array 结果数组
 * */
function xml_to_array($xml){
    if(!$xml){
        return false;
    }

    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if(preg_match_all($reg, $xml, $matches)){
        $count = count($matches[0]);
        for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
            if(preg_match( $reg, $subxml )){
                $arr[$key] = xml_to_array( $subxml );
            }else{
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}
/**
 * 字条串截取
 * @param type $text 字符串信息
 * @return type $length 长度
 * */
function subtext($text, $length){
    if(mb_strlen($text, 'utf8') > $length) 
    return mb_substr($text, 0, $length, 'utf8').'...';
    return $text;
}
/**
* 删除图片
* $praram $url 图片路径
*/
function del_img($url =''){
	if($url){
		@unlink(APP_PATH.$url);
	}
}